package com.hldsmart.smartda.adapter.home;


/**
 * Created by liuzhihui on 2017/1/7.
 */

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.model.entity.ShopEntity;
import com.hldsmart.smartda.model.entity.TypesEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lzh194 on 2017/1/16.
 * 首页适配器
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<ShopEntity> list = new ArrayList<>();
    private OnItemClickedListener listener;


    private TypeAdapter typeAdapter;


    public void addData(List<ShopEntity> data) {
        list.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return 1;
        else if (position == 1)
            return 2;
        return super.getItemViewType(position);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view;
        if (viewType == 1) {
            view = LayoutInflater.from(context).inflate(R.layout.rv_type_parent_item, parent, false);
            return new TypeViewHolder(view);
        }else if (viewType == 2) {
            view = LayoutInflater.from(context).inflate(R.layout.rv_like_item, parent, false);
            return new LikeViewHolder(view);
        }
            view = LayoutInflater.from(context).inflate(R.layout.rv_list_parent_item, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof TypeViewHolder) {
            TypeViewHolder typeViewHolder = (TypeViewHolder) holder;

            typeAdapter = new TypeAdapter();
            ArrayList<TypesEntity> types = new ArrayList<>();
            //  安全手环  智能助手、 我的足迹、 扫一扫、  智能推荐、个性定制、团购、优惠券
//         "securityBracelet"SmartSettings、myFootprint、scan、smartRecommend、individuation、groupBuy、discountTicket
            types.add(new TypesEntity("安全手环", "securityBracelet", R.drawable.img_all));
            types.add(new TypesEntity("智能助手", "smartSetting", R.drawable.img_household));
            types.add(new TypesEntity("我的足迹", "myFootprint", R.drawable.img_electron));
            types.add(new TypesEntity("扫一扫", "scan", R.drawable.img_dress));
            types.add(new TypesEntity("智能推荐", "smartRecommend", R.drawable.img_toys));
            types.add(new TypesEntity("个性定制", "individuation", R.drawable.img_book));
            types.add(new TypesEntity("现场团", "groupBuy", R.drawable.img_gift));
            types.add(new TypesEntity("优惠券", "discountTicket", R.drawable.img_other));

            typeAdapter.setData(types);
            typeViewHolder.rv_type_parent.setLayoutManager(new GridLayoutManager(context,4));
            typeViewHolder.rv_type_parent.setAdapter(typeAdapter);

        }
        if (holder instanceof LikeViewHolder){
            LikeViewHolder likeViewHolder = (LikeViewHolder) holder;

        }

        if (holder instanceof ListViewHolder) {
            ListViewHolder listViewHolder = (ListViewHolder) holder;
            listViewHolder.name.setText(list.get(position - 2).getName());
            listViewHolder.price.setText("团购价:￥"+list.get(position - 2).getPrice());
            listViewHolder.oldPrice.setText("原    价:￥"+list.get(position - 2).getOldprice());
            listViewHolder.describe.setText(list.get(position - 2).getDescribe());
            listViewHolder.im.setImageResource(list.get(position - 2).getPage());

//            listViewHolder.activity.setText(list.get(position - 2).);

//            listViewHolder.ll_list_good.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (listener != null) {
//                        listener.onPhotoClicked(list.get(position - 2));
//                    }
//                }
//            });
//            ImageLoader.getInstance().displayImage(ShopApi.IMAGE_URL + list.get(position-2).getPage(),
//                    listViewHolder.im, ImageLoadOptions.build_item());
        }
    }


    @Override
    public int getItemCount() {
        return list.size() + 2;
    }


    public void setListener(OnItemClickedListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickedListener {
        void onPhotoClicked(ShopEntity shopEntity);
    }

    class TypeViewHolder extends RecyclerView.ViewHolder {
        public
        @Bind(R.id.rv_type_parent)
        RecyclerView rv_type_parent;

        public TypeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

    class LikeViewHolder extends RecyclerView.ViewHolder {
        public
        @Bind(R.id.tv_like_group)
        TextView tv_like_group;
        @Bind(R.id.tv_like_shop)
        TextView tv_like_shop;

        public LikeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

    class ListViewHolder extends RecyclerView.ViewHolder {


        @Bind(R.id.iv_image)
        ImageView im;

        @Bind(R.id.tv_shopName)
        TextView name;

        @Bind(R.id.tv_describe)
        TextView describe;

        @Bind(R.id.tv_price)
        TextView price;

        @Bind(R.id.tv_oldPrice)
        TextView oldPrice;

        @Bind(R.id.tv_activity)
        TextView activity;

        @Bind(R.id.ll_list_shop)
        LinearLayout ll_list_shop;

        public ListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

}
